var currentSelectProvinceName = "";
var d = new Date();
var hour = d.getHours();
var ua = navigator.userAgent.toLowerCase();
var theProvs = [];
theProvs[10] = "A 安徽";
theProvs[11] = "A 澳门";
theProvs[12] = "B 北京";
theProvs[43] = "C 重庆";
theProvs[13] = "F 福建";
theProvs[14] = "G 甘肃";
theProvs[15] = "G 广东";
theProvs[16] = "G 广西";
theProvs[17] = "G 贵州";
theProvs[18] = "H 海南";
theProvs[19] = "H 河北";
theProvs[20] = "H 河南";
theProvs[21] = "H 黑龙江";
theProvs[22] = "H 湖北";
theProvs[23] = "H 湖南";
theProvs[24] = "J 吉林";
theProvs[25] = "J 江苏";
theProvs[26] = "J 江西";
theProvs[27] = "L 辽宁";
theProvs[28] = "N 内蒙古";
theProvs[29] = "N 宁夏";
theProvs[30] = "Q 青海";
theProvs[31] = "S 山东";
theProvs[32] = "S 山西";
theProvs[33] = "S 陕西";
theProvs[34] = "S 上海";
theProvs[35] = "S 四川";
theProvs[37] = "T 天津";
theProvs[36] = "T 台湾";
theProvs[38] = "X 西藏";
theProvs[39] = "X 香港";
theProvs[40] = "X 新疆";
theProvs[41] = "Y 云南";
theProvs[42] = "Z 浙江";
var timeout = "";
var submitByEnterBtn = false;
//var searchInputTxt = "中文、拼音、电话区号  搜索城市/景点";
var searchInputTxt = "中文、拼音 搜索国内地区";
if (typeof(pageType) == "undefined"){
    pageType = '';
}
function initSearchInput() {
    if (typeof(pageType) != "undefined" && pageType == "foreign") {
        searchInputTxt = "中文、英文 搜索国际城市"
    } else if (typeof(pageType) != "undefined" && pageType == "jingdian") {
        searchInputTxt = "请输入中文搜索景点"
    }else {
        if (typeof(pageType) != "undefined" && pageType == "timezone") {
            searchInputTxt = "中文、英文  搜索世界时间"
        }
    }
    $("#searchForm").submit(function(a) {
        if ($("#searchResult a.ui-state-error").length == 0 && $("#searchResult a.ui-state-selected").length > 0 && $("#inputSearchCity").val() != searchInputTxt) {
            window.location.href = $("#searchResult a.ui-state-selected").attr("href");
            return false
        }
        submitByEnterBtn = true;
        startSearch();
        a.preventDefault();
        return false
    });
    $("#inputSearchCity").keydown(function(b) {
        clearTimeout(timeout);
        keycode = b.which;
        if (keycode != 40 && keycode != 38) {
            submitByEnterBtn = false;
            timeout = setTimeout(startSearch, 250)
        }
    }).click(function(c) {
        if ($(".ui-state-selected").length > 0) {
            $(".ui-state-selected").removeClass("ui-state-selected")
        }
        var e = $("#inputSearchCity").val();
        if (e != searchInputTxt && e != "") {
            $("#searchForm").submit();
            return false
        }
        if (typeof(pageType) !== "undefined" && pageType == "timezone") {
            return false
        }
        isCurrentShowing = false;
        mytoggleLayer(isCurrentShowing, $("#citySelect"), $("#citySelect"));
        cancelEvent(c);
        return false
    }).focus(function() {
        if ($(this).val() == searchInputTxt) {
            $(this).val("").css("color", "#333")
        }
    }).blur(function() {
        if ($(this).val().replace(/^\s*$/, "") == "") {
            $(this).val(searchInputTxt).css("color", "#999")
        }
    }).val(searchInputTxt);
    startAutocomplete()
}

function getRegionPinyin(k){
     $.ajax({
        url: "/home/info/getPinAjax",
        data: {
            id: k
        },
        async: true,
        dataType: "text",
        success: function(f) {
            setC(k);
            window.location.href = "/"+f ;
       }
    });
}
function startSearch() {
    if ($("#inputSearchCity").val().replace(/\?/, "") == "") {
        $("#inputSearchCity").trigger("click");
        return
    }
    if (submitByEnterBtn && !$("#inputSearchCity").autocomplete("widget").is(":visible")) {
        $("#inputSearchCity").autocomplete("search", $("#inputSearchCity").val());
        $("#searchResult").addClass("suggest-list-open");
        $("#searchResult ul").show();
        return false
    }
}
function startAutocomplete() {
    $("#inputSearchCity").autocomplete({
        source: function(b, a) {
            $.ajax({
                url: "/search/index",
                data: {
                    k: $("#inputSearchCity").val(),
                    t: pageType
                },
                dataType: "json",
                success: function(f) {
                    // alert(f.length); return false;
                    if (f == null) {
                        var e = "对不起，未找到您查询的地点天气。";
                        var c = [{
                            error: true,
                            text: e
                        }];
                        a(c)
                    }else{
                        a($.map(f,
                        function(g) {
                            if(typeof(pageType) !== "undefined" && pageType == "jingdian"){
                                return {
                                    href: '/jingdian/'+g.id,
                                    text: g.name+'-'+g.area+'-<b>'+g.prov+'</b>'
                                };
                            }else if(typeof(pageType) !== "undefined" && pageType == "foreign"){
                                return {
                                    href: '/foreigndetail/'+g.id,
                                    text: g.city+'-'+g.country+'-<b>'+g.py+'</b>'
                                };
                            }else{
                                return {
                                    href: '/'+g.realpy,
                                    classid: g.id,
                                    text: g.city+'-'+g.prov+'-<b>'+g.pinyin+'</b>'
                                };
                            }
                        }))
                    }
                }
            })
        },
        appendTo: ".suggest-list",
        position: {
            my: "left-1 top",
            at: "left bottom"
        },
        delay: 300,
        autoFocus: true,
        select: function(a, b) {
            if (b.item != undefined && !b.item.error) {
                window.location.href = b.item.href
            } else {
                return false
            }
        },
        create: function(a, b) {
            $(".ui-helper-hidden-accessible").remove()
        },
        focus: function(a, b) {
            if ($(".ui-state-focus").length > 0) {
                $(".ui-state-focus").addClass("ui-state-selected");
                $(".ui-state-focus").parent().siblings().children(".ui-state-selected").removeClass("ui-state-selected")
            }
        },
        open: function(a, b) {
            $("#searchResult").addClass("suggest-list-open");
            $("#citySelect").hide()
        }
    });
    $("#inputSearchCity").data("autocomplete")._renderItem = function(c, e) {
        var a = $("#inputSearchCity").val();
        e.label = a;
        e.value = a;
        if (e.error) {
            return $("<li>").append($("<a>").attr("class", "ui-state-error").html(e.text)).appendTo(c)
        } else {
            if (/[a-zA-Z]+/.test(a) && /span/.test(e.text)) {
                var f = e.text.split("<span>");
                if (f.length == 2) {
                    e.text = f[0].replace(a, "<b>" + a + "</b>") + "<span>" + f[1]
                }
            } else {
                e.text = e.text.replace(a, "<b>" + a + "</b>")
            }
            if(e.classid != undefined){
                return $("<li>").append($("<a>").bind("click", function() {setC(e.classid);}).attr("href", e.href).html(e.text)).appendTo(c)
            }else{
                return $("<li>").append($("<a>").attr("href", e.href).html(e.text)).appendTo(c)
            }
        }
    };
    $("#inputSearchCity").data("autocomplete")._renderMenu = function(b, a) {
        var c = this;
        $.each(a,
        function(e, f) {
            c._renderItemData(b, f)
        });
        if (a.length < 10) {
            $(b).css("height", 26 * a.length)
        } else {
            $(b).css("height", 260)
        }
    }
}
function hidePoplayer() {
    $(".pop-layer").hide();
    $(".arrow").removeClass("uarr");
    $(".pm-info").removeClass("cur");
    $(".suggest-list").removeClass("suggest-list-open");
    $(".select-city").removeClass("select-city-hover")
}
function gotoMyHref(a) {
    window.location.href = a.attr("href")
}
function blanktoHref(a) {
    window.open(a.attr("href"))
}
function mytoggleLayer(a, c, b) {
    hidePoplayer();
    if (a) {
        c.hide();
        b.removeClass("uarr").removeClass("select-city-hover")
    } else {
        c.show();
        b.addClass("uarr").addClass("select-city-hover")
    }
}
function bindInitEvent() {
    $(document).click(function(a) {
        hidePoplayer()
    });
    $(".pop-layer").click(function(a) {
        cancelEvent(a);
        return false
    });
    $("#province a").click(function() {
        if (match = $(this).attr("href").match(/#(\d+)/)) {
            provId = match[1];
            cities = getCitiesByProvId(provId);
            displaySearchDiv(cities, "city", provId, $(this).html());
            currentSelectProvinceName = $(this).html();
            bindCitySelect();
            bindCountySelect()
        }
    });
    ipJudgeError()
}
//温馨提示
function aqiGuide(a) {
    aqiGuides = {
        "1": "多参加户外活动，呼吸清新空气啦。",
        "2": "出去走走放松心情，感受美好大自然吧。",
        "3": "敏感体质的朋友要减少外出哦！",
        "4": "敏感体质的朋友尽量不要出门啦！",
        "5": "为了健康着想，各位朋友尽量减少外出吧！",
        "6": "请尽量避免外出，外面红绿灯都看不见呢！",
        "7": "请尽量避免外出，外面红绿灯都看不见呢！"
    };
    if (typeof(aqiGuides[a]) != "undefined") {
        return aqiGuides[a]
    } else {
        return "暂无"
    }
}

// 7天气温走势 
function sevenTemp() {
    html = "";
    for (i = 0; i < 7; i++) {
        var b = "";  
        html += "<li" + (i == 0 ? ' class="current"': "") + '><div class="hd">' + data[i]["weeks"] + '</div>' +  "</li>"
    }
    $("#sevenTemp ul").html(html);
    todayFutureTempWidget()
}
function todayFutureTempWidget() {
    var a = new dhtmlXChart({
        view: "line",
        container: "todayFutureTemp",
        value: "#wenduB#",
        label: "#curB##wenduB#",
        tooltip: {
            template: "#curB##wenduB#"
        },
        item: {
            borderColor: "#ff8f21",
            color: "#ff8f21"
        },
        line: {
            color: "#ff8f21",
            width: 3
        },
        padding: {
            left: 0,
            bottom: 20,
            right: 0
        }
    });
    a.addSeries({
        value: "#wenduH#",
        label: "#curH##wenduH#",
        tooltip: {
            template: "#curH##wenduH#"
        },
        item: {
            borderColor: "#66d53f",
            color: "#66d53f"
        },
        line: {
            color: "#66d53f",
            width: 3
        }
    });
    a.parse(data, "json")
}

// 24小时整点
function todayHourWidget() {
    var c = new Date();
    var a = c.getHours();
    html = "";
    for (i = 0; i < wea_h_hour.length; i++) {
        var b = "";
        if (i <= 5 || i >= 19) {
            b = "wnt_l"
        }
        html += "<li" + (i == a ? ' class="current"': "") + '><div class="hd">' + wea_h_hour[i]["hour"] + '点</div><div class="bd"><p class="f_ico"><span class="wea_l w' + wea_h_hour[i]["icon"] + "_l " + b + '"></span></p><p class="f_wz">' + wea_h_hour[i]["tq"] + '</p></div><div class="ft">' + wea_h_hour[i]["temp"] + "℃</div></li>"
    }
    $("#wea_hour ul").html(html);
    hour24Slide()
}
function hour24Slide() {
    var e = $("#wea_hour ul"),
    b = $("#wea_hour .tab-body .current").index(),
    f = $("#hourPrev"),
    c = $("#hourNext"),
    a = [0, -598, -1193,-1787],
    g = 0;
    if (b <= 5) {
        g = 0;
        f.addClass("disable")
    }
    if (b >= 6 && b <= 11) {
        g = 1
    }
    if (b >= 12 && b <= 17) {
        g = 2
    }
    if (b >= 18 && b <= 23) {
        g = 3;
        c.addClass("disable")
    }
    e.css("margin-left", a[g] + "px");
    f.bind("click",
    function() {
        if ($(this).hasClass("disable")) {
            return
        } else {
            if (c.hasClass("disable")) {
                c.removeClass("disable")
            }
            g--;
            e.animate({
                marginLeft: a[g] + "px"
            },
            200, "linear");
            if (g <= 0) {
                $(this).addClass("disable")
            }
        }
    });
    c.bind("click",
    function() {
        if ($(this).hasClass("disable")) {
            return
        } else {
            if (f.hasClass("disable")) {
                f.removeClass("disable")
            }
            g++;
            e.animate({
                marginLeft: a[g] + "px"
            },
            200, "linear");
            if (g >= 2) {
                $(this).addClass("disable")
            }
        }
    })
}
// loadJs
function loadJs(b) {
    var e = arguments[1] ||
    function() {};
    var c = arguments[2] || "UTF-8";
    var a = document.createElement("script");
    a.setAttribute("type", "text/javascript");
    a.setAttribute("charset", c);
    a.setAttribute("defer", true);
    a.setAttribute("src", b);
    if (document.all) {
        a.onreadystatechange = function() {
            if (this.readyState == "loaded" || this.readyState == 4 || this.readyState == "complete") {
                setTimeout(e, 10)
            }
        }
    } else {
        a.onload = function() {
            e()
        }
    }
    document.getElementsByTagName("head")[0].appendChild(a)
}
function loadJsInterhour(b) {
    var f = arguments[1] ||
    function() {};
    var e = "UTF-8";
    var c = arguments[2];
    var a = document.createElement("script");
    a.setAttribute("type", "text/javascript");
    a.setAttribute("charset", e);
    a.setAttribute("defer", true);
    a.setAttribute("src", b);
    if (document.all) {
        a.onreadystatechange = function() {
            if (this.readyState == "loaded" || this.readyState == 4 || this.readyState == "complete") {
                setTimeout(f, 10)
            }
        }
    } else {
        a.onload = function() {
            f(c)
        }
    }
    document.getElementsByTagName("head")[0].appendChild(a)
}



